// 引入 react 依赖
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Text, View, Image, TouchableOpacity, ImageBackground, ScrollView, InteractionManager } from 'react-native';

// 引入第三方依赖
import _ from 'lodash';
import pureRender from 'pure-render-decorator';
import { Actions } from 'react-native-router-flux';
import Icon from 'react-native-vector-icons/Ionicons';

// antd 按需加载
import { Tabs, Toast, Portal } from '@ant-design/react-native';

// 引入 处理数据 所需文件
import { myFollowAccount } from '../../module/module.js'

// 引入样式
import Style from './style/style.js';

// 引入公用组件
import { Status, NavBar, CacheImage, RnFlatList, FansItem } from '../../components/index.js'

// 引入专用组件
import { } from './components'

// 引入工具包
import { } from '../../util/images.js';
import { press, size, ScreenWidth, borderBottom, pathHead, defaultDataList, onEndReached, onRefresh, concatDataList, defaultPage } from '../../util/utils.js'

class FollowAccount extends Component {
  constructor(props) {
    super(props)
    this.state = {
      refreshing: false,
      searchBackgroundColor: '#ccc',
      loading: false,
      type: 1,
      detail: null,
      dataList: defaultDataList
    }
  }

  componentDidMount() {
    InteractionManager.runAfterInteractions(() => {
      // ...耗时较长的同步的任务...
      // this.showLoadingData()
      this.loading = Toast.loading('加载中', 0)
      this.handleData().finally(() => { Portal.remove(this.loading) })
    });
  }

  showLoadingData(options) {
    this.loading = Toast.loading('加载中', 0)
    this.loadingData(options).then(res => {
      this.setState({
        detail: res[0].data
      })
    }).finally(() => {
      Portal.remove(this.loading)
    })
  }

  loadingData(options) {
    const { id } = this.props
    return Promise.all([plateDetail({ id })])
  }

  handleData(options) {
    const { id } = this.props
    const { type } = this.state
    return new Promise((resolve, reject) => {
      myFollowAccount({
        ...defaultPage.call(this),
        ...options
      }).then(res => {
        resolve(res)
        concatDataList.call(this, res)
      }).catch(error => {
        reject(error)
        console.log(error, 11111)
      })
    }).catch(error => {
      console.error(error)
    })
  }

  onRefresh = () => {
    onRefresh.call(this)
  }

  onEndReached = () => {
    onEndReached.call(this)
  }

  onScroll = (e) => {
    const { searchBackgroundColor } = this.state
    if (e.nativeEvent.contentOffset.y >= size(366) - 44) {
      this.setState({
        searchBackgroundColor: '#e43130'
      })
    } else {
      this.setState({
        searchBackgroundColor: 'transparent'
      })
    }
  }

  render() {
    const { refreshing, dataList } = this.state
    const { cv } = this.props
    return (
      <View style={Style.container}>
        <Status />
        <NavBar
          title="关注的人"
          back={true}
        />
        <RnFlatList
          style={{
            flex: 1,
            backgroundColor: '#f0f2f5',
          }}
          onScroll={this.onScroll}
          refreshing={refreshing}
          onRefresh={this.onRefresh}
          onEndReached={this.onEndReached}
          removeClippedSubviews={true}
          data={dataList}
          renderItem={({ item }) => {
            return (
              <FansItem
                data={item}
              />
            )
          }}
        />
      </View >
    );
  }
}


export default connect(({ user }) => ({
  user,
}))(pureRender(FollowAccount))
